package de.baumann.browser.activitys.hash

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.support.v7.widget.GridLayoutManager
import android.view.View
import de.baumann.browser.R
import de.baumann.browser.activitys.BaseFileUploadActivity
import de.baumann.browser.adapter.ImageListAdapter
import de.baumann.browser.iview.ISendPackView
import de.baumann.browser.present.SendPackPresenter
import kotlinx.android.synthetic.main.activity_send_pack.*
import kotlinx.android.synthetic.main.include_tool_bar.*
import java.util.*

/**
 *@author:junce
 *@date:2019/3/5 10:27
 *@discription: 发红包
 */

class SendPackActivity : BaseFileUploadActivity<ISendPackView,SendPackPresenter>(),
        View.OnClickListener,
        ImageListAdapter.OnClickAddImageListener,
        ISendPackView {

    companion object {
        const val PACK_URL_CODE = 1024
        const val PACK_LOCATION_CODE = 1025
        fun startSendPackActivity(context: Context) {
            context.startActivity(Intent(context, SendPackActivity::class.java))
        }
    }


    private var isUpload = false

    private var imageUrl = ""
    private var packUrl = ""
    private lateinit var location: String

    private lateinit var imageListAdapter: ImageListAdapter

    override fun getTitleText(): String {
        return getString(R.string.send_red_packet)
    }

    override fun getLayout(): Int {
        return R.layout.activity_send_pack
    }

    override fun createPresenter(): SendPackPresenter {
        return SendPackPresenter()
    }

    override fun createView(): ISendPackView {
        return this
    }

    override fun initView() {
        initToolbar(commToolBar, commTitle)

        llPackUrl.setOnClickListener(this)
        llPackLocation.setOnClickListener(this)
        btnSend.setOnClickListener(this)

        rvPackImage.layoutManager = GridLayoutManager(mContext, 4)
        imageListAdapter = ImageListAdapter(mContext, this)
        rvPackImage.adapter = imageListAdapter
    }

    override fun initData() {
        location = resources.getString(R.string.national)
    }

    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.llPackUrl -> startActivityForResult(Intent(mContext, PackUrlActivity::class.java), PACK_URL_CODE)
            R.id.llPackLocation -> startActivityForResult(Intent(mContext, PackLocationActivity::class.java), PACK_LOCATION_CODE)
            R.id.btnSend -> {
                presenter.sendPack()
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (resultCode != Activity.RESULT_OK) {
            return
        }
        when (requestCode) {
            PACK_URL_CODE -> {
                if (data != null) {
                    packUrl = data.getStringExtra("url")
                }
                tvPackUrl.text = packUrl
            }

            PACK_LOCATION_CODE -> {
                if (data != null) {
                    location = data.getStringExtra("location")
                }
                tvPackLocation.text = location
            }

            else -> {
                super.onActivityResult(requestCode, resultCode, data)
            }
        }
    }

    override fun clickAddImage() {
        showModeSelectDialog()
    }

    override fun setBitmap(bitmapPath: String?) {
        imageListAdapter.addData(bitmapPath)
    }

    override fun uploadSuccess(url: String?) {
    }

    override fun uploadSuccess(pathMap: LinkedHashMap<String, String>?) {
        isUpload = true
        val buffer = StringBuffer()
        if (pathMap != null) {
            for ((key) in pathMap) {
                buffer.append("https://img.nearu.vip/")
                buffer.append(pathMap[key])
                buffer.append(",")
            }
        }
        imageUrl = buffer.deleteCharAt(buffer.length - 1).toString()
        presenter.sendPack()
    }

    override fun uploadFailed(url: String?) {
        isUpload = false
    }

    override fun getPackDesc(): String {
        return edPackDesc.text.toString().trim()
    }


    override fun getPackImageUrls(): String {
        if (!isUpload) {
            val datas = imageListAdapter.datas
            if (datas.isNotEmpty()) {
                imageUrl = datas[0]
            }
        }
        return imageUrl
    }

    override fun getPackAmount(): String {
        return edPackAmount.text.toString().trim()
    }

    override fun getPackCount(): String {
        return edPackNum.text.toString().trim()
    }

    override fun getPackAdUrl(): String {
        return packUrl
    }

    override fun getPackAdLocation(): String {
        return location
    }

    override fun sendPackSuccess() {
        showShortToast(getString(R.string.Red_envelope_send_successfully))
        finish()
    }

    override fun getIsUpload(): Boolean {
        return isUpload
    }

    override fun uploadImage(token: String) {
        uploadToQiNiu(imageListAdapter.datas, token)
    }

}
